﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JustMVCTest.Models;
//using Telerik.Web.Mvc.Examples.Models;
using Telerik.Web.Mvc;
using System.Text;
using System.IO;

using System.Collections;
using Telerik.Web.Mvc.Extensions;
//using Telerik.Web.Mvc.Examples.Models;
using System.Diagnostics;


namespace JustMVCTest.Controllers
{
    /*
    [SourceCodeFile("EditableProduct (model)", "~/Models/EditableProduct.cs")]
    [SourceCodeFile("SessionProductRepository", "~/Models/SessionProductRepository.cs")]
    [SourceCodeFile("Date.ascx (editor)", "~/Views/Shared/EditorTemplates/Date.ascx")]
    public ActionResult EditingAjax(GridEditMode? mode, GridButtonType? type, GridInsertRowPosition?
        insertRowPosition)
    {
        ViewData["mode"] = mode ?? GridEditMode.InLine;
        ViewData["type"] = type ?? GridButtonType.Text;
        ViewData["insertRowPosition"] = insertRowPosition ?? GridInsertRowPosition.Top;
        return View();
    }
    */

    
    public class MaintenanceController : Controller
    {
        private PODBContext db = new PODBContext();

        
        public string LaunchPapiImport(int workmode)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = @"C:/webs/JustMVCTest/PAPI_Import/bin/Debug/PAPI_Import.exe";
            psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName);
            psi.Arguments = workmode.ToString();
            System.Diagnostics.Process.Start(psi);
            return "Launched at: " + System.DateTime.Now + ". Workmode: " + workmode;
        }

        public string LaunchNotepad()
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = @"C:/webs/JustMVCTest/PAPI_Import/notepad.exe";
            psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName);
            System.Diagnostics.Process.Start(psi);
            return "Notepad Launched at: " + System.DateTime.Now;
        }

        [HttpPost]
        public ActionResult serverJsonMethod()
        {
            // TODO: save the comment
            return Json(new { someProperty = "some value" });
        }

        
        public ActionResult Index()
        {
            WrapperModel wrapperModel = new WrapperModel();
            //wrapperModel.CostCenter = new List<CostCenter>();
            //wrapperModel.InternalOrder = new List<InternalOrder>();
            wrapperModel.Papi_Users = new List<Papi_Users>();
            //wrapperModel.poland_Rep_Table = new List<Poland_Rep_Table>();
            wrapperModel.pO = new List<PO>();
            return View(wrapperModel);
        }

        
        //Papi_Users

        [GridAction]
        public ActionResult Papi_UsersSelectAjaxEditing()
        {
            return View(new GridModel(db.Papi_Users_tbl));
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Papi_UsersSaveAjaxEditing(String id)
        {
            Papi_Users papi_users = db.Papi_Users_tbl.Find(id);

            //Customer customer = CustomerRepository.FindById(id);

            if (papi_users != null)
            {
                //Perform model binding (fill the customer properties and validate it).
                if (TryUpdateModel(papi_users))
                {
                    //The model is valid - update the customer and redisplay the grid.

                    db.SaveChanges();
                    //CustomerRepository.Update(papi_users);
                }
            }

            //Rebind the grid
            return View(new GridModel(db.Papi_Users_tbl));
        }


        public ActionResult Papi_UsersExportTableToCSV()
        {
            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
            writer.Write("domain_username,");
            writer.Write("email,");
            writer.Write("First_Name,");
            writer.Write("Last_Name,");
            writer.Write("Requisitio,");
            writer.Write("is_admin,");
            writer.Write("is_superuser,");
            writer.Write("is_tssc,");
            writer.Write("Cost_Cent,");
            writer.Write("Int_Order,");
            writer.Write("filter");
            writer.WriteLine();
            foreach (Papi_Users papi_users in db.Papi_Users_tbl)
            {
                writer.Write(papi_users.domain_username);       writer.Write(",");
                writer.Write(papi_users.email);                 writer.Write(",");
                writer.Write(papi_users.First_Name);            writer.Write(",");
                writer.Write(papi_users.Last_Name);             writer.Write(",");
                writer.Write(papi_users.Requisitio);            writer.Write(",");
                writer.Write(papi_users.is_admin);              writer.Write(",");
                writer.Write(papi_users.is_superuser);          writer.Write(",");
                writer.Write(papi_users.is_tssc);               writer.Write(",");
                writer.Write(papi_users.Cost_Cent);             writer.Write(",");
                writer.Write(papi_users.Int_Order);             writer.Write(",");
                writer.Write(papi_users.filter);                writer.WriteLine();            
            }
            writer.Flush();
            output.Position = 0;

            return File(output, "text/comma-separated-values", "papi_users.csv");
        }
        
        public ActionResult Papi_UsersExportCurrentToCSV(int page, string orderBy, string filter)
        {
            //IEnumerable orders = GetOrders().AsQueryable().ToGridModel(page, 10, orderBy, string.Empty, filter).Data;

            IEnumerable users = db.Papi_Users_tbl.AsQueryable().ToGridModel(page, 10, orderBy, string.Empty, filter).Data;

            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
            writer.Write("domain_username,");
            writer.Write("email,");
            writer.Write("First_Name,");
            writer.Write("Last_Name,");
            writer.Write("Requisitio,");
            writer.Write("is_admin,");
            writer.Write("is_superuser,");
            writer.Write("is_tssc,");
            writer.Write("Cost_Cent,");
            writer.Write("Int_Order,");
            writer.Write("filter");

            writer.WriteLine();
            foreach (Papi_Users papi_users in users)
            {
                writer.Write(papi_users.domain_username);       writer.Write(",");
                writer.Write(papi_users.email);                 writer.Write(",");
                writer.Write(papi_users.First_Name);            writer.Write(",");
                writer.Write(papi_users.Last_Name);             writer.Write(",");
                writer.Write(papi_users.Requisitio);            writer.Write(",");
                writer.Write(papi_users.is_admin);              writer.Write(",");
                writer.Write(papi_users.is_superuser);          writer.Write(",");
                writer.Write(papi_users.is_tssc);               writer.Write(",");
                writer.Write(papi_users.Cost_Cent);             writer.Write(",");
                writer.Write(papi_users.Int_Order);             writer.Write(",");
                writer.Write(papi_users.filter);                writer.WriteLine();
            }
            writer.Flush();
            output.Position = 0;

            return File(output, "text/comma-separated-values", "papi_users.csv");
        }

        
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Papi_UsersInsertAjaxEditing()
        {
            //Create a new instance of the EditableProduct class.

            Papi_Users papi_users = new Papi_Users();
            //EditableProduct product = new EditableProduct();
            
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(papi_users))
            {
                //The model is valid - insert the product.
                db.Papi_Users_tbl.Add(papi_users);
                db.SaveChanges();
                //SessionProductRepository.Insert(product);
            }
            //Rebind the grid
            return View(new GridModel(db.Papi_Users_tbl));
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Papi_UsersDeleteAjaxEditing(String id)
        {
            Papi_Users papi_users = db.Papi_Users_tbl.Find(id);

            if (papi_users != null)
            {
                db.Papi_Users_tbl.Remove(papi_users);
                
                if (TryUpdateModel(papi_users))
                {
                    db.SaveChanges();
                }
            }
            return View(new GridModel(db.Papi_Users_tbl));
        }

        // POes
        [GridAction]
        public ActionResult POesSelectAjaxEditing()
        {
            return View(new GridModel(db.POs));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult POesSaveAjaxEditing(long PONumber,int item,int update_num)
        {
            PO pO = db.POs.Find(PONumber, item, update_num);
            if (pO != null)
            {
                if (TryUpdateModel(pO))
                {
                    db.SaveChanges();
                }
            }
            return View(new GridModel(db.POs));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult POesInsertAjaxEditing()
        {
            PO pO = new PO();
            if (TryUpdateModel(pO))
            {
                db.POs.Add(pO);
                db.SaveChanges();
            }
            return View(new GridModel(db.POs));
        }

        /*
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult POesDeleteAjaxEditing(String id)
        {
            int x = 2;

            return View(new GridModel(db.POs));
        }
        */
        
        
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult POesDeleteAjaxEditing(long PONumber, int Item, int Update_Num)
        {
            /*
            var pO_ToDelete = db.POs.First(m => m.PONumber == PONumber && m.Item == Item && m.Update_Num == m.Update_Num);
            db.POs.Remove(pO_ToDelete);
            db.SaveChanges();
            return View(new GridModel(db.POs));
             */

            PO pO = db.POs.Find(PONumber, Item, Update_Num);
            
            if (pO != null)
            {
                db.POs.Remove(pO);
                if (TryUpdateModel(pO))
                {
                    db.SaveChanges();
                }
            }
            return View(new GridModel(db.POs));
             
        }   
    
    }
         
}

    



